package com.ideabove.radio.module;

import com.ideabove.radio.MainActivity;
import com.ideabove.radio.R;
import com.ideabove.radio.module.FacebookMd.FacebookCallback;
import com.ideabove.radio.service.PlayBackService;
import com.ideabove.radio.service.PlayBackService.MediaPlayerSingleton;
import com.ideabove.util.Connectivity;
import com.tekle.oss.android.animation.AnimationFactory;
import com.tekle.oss.android.animation.AnimationFactory.FlipDirection;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.PorterDuff.Mode;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class GenreMd  extends ModuleAbs{

	public void init(MainActivity activity){
		super.init(activity);

		// Channel
		ImageView player_btn_play = (ImageView) activity.findViewById(R.id.img_genre_latin);
		player_btn_play.setOnClickListener(activity);
		LinearLayout radio_every_music = (LinearLayout) activity.findViewById(R.id.radio_every_music);
		radio_every_music.setOnClickListener(activity);		
		LinearLayout radio_ch_hotsong = (LinearLayout) activity.findViewById(R.id.radio_ch_hotsong);
		radio_ch_hotsong.setOnClickListener(activity);		
		LinearLayout radio_ch_kpophotsong = (LinearLayout) activity.findViewById(R.id.radio_ch_kpophotsong);
		radio_ch_kpophotsong.setOnClickListener(activity);		
		LinearLayout radio_ch_bonoflowfm = (LinearLayout) activity.findViewById(R.id.radio_ch_bonoflowfm);
		radio_ch_bonoflowfm.setOnClickListener(activity);		

		// genre
		ImageView img_genre_dence = (ImageView) activity.findViewById(R.id.img_genre_dence);
		img_genre_dence.setOnClickListener(activity);
		ImageView img_genre_electronic = (ImageView) activity.findViewById(R.id.img_genre_electronic);
		img_genre_electronic.setOnClickListener(activity);
		ImageView img_genre_house = (ImageView) activity.findViewById(R.id.img_genre_house);
		img_genre_house.setOnClickListener(activity);
		ImageView img_genre_pop = (ImageView) activity.findViewById(R.id.img_genre_pop);
		img_genre_pop.setOnClickListener(activity);
		ImageView img_genre_rb = (ImageView) activity.findViewById(R.id.img_genre_rb);
		img_genre_rb.setOnClickListener(activity);
		ImageView img_genre_jazz = (ImageView) activity.findViewById(R.id.img_genre_jazz);
		img_genre_jazz.setOnClickListener(activity);
		ImageView img_genre_hiphop = (ImageView) activity.findViewById(R.id.img_genre_hiphop);
		img_genre_hiphop.setOnClickListener(activity);
		ImageView img_genre_rock = (ImageView) activity.findViewById(R.id.img_genre_rock);
		img_genre_rock.setOnClickListener(activity);
		ImageView img_genre_metal = (ImageView) activity.findViewById(R.id.img_genre_metal);
		img_genre_metal.setOnClickListener(activity);
		ImageView img_genre_classical = (ImageView) activity.findViewById(R.id.img_genre_classical);
		img_genre_classical.setOnClickListener(activity);
		ImageView img_genre_newage = (ImageView) activity.findViewById(R.id.img_genre_newage);
		img_genre_newage.setOnClickListener(activity);
		ImageView img_genre_folk = (ImageView) activity.findViewById(R.id.img_genre_folk);
		img_genre_folk.setOnClickListener(activity);
		ImageView img_genre_latin = (ImageView) activity.findViewById(R.id.img_genre_latin);
		img_genre_latin.setOnClickListener(activity);
		ImageView img_genre_reggage = (ImageView) activity.findViewById(R.id.img_genre_reggage);
		img_genre_reggage.setOnClickListener(activity);
		ImageView img_genre_reli = (ImageView) activity.findViewById(R.id.img_genre_reli);
		img_genre_reli.setOnClickListener(activity);
		ImageView img_genre_other = (ImageView) activity.findViewById(R.id.img_genre_other);
		img_genre_other.setOnClickListener(activity);

	}

	public void showGenreList(){
		// 모듈을 셋함
		activity.setCurrentMoudle(this);		
		View view = (View)activity.findViewById(R.id.flipper_layout);
		view.setVisibility(View.VISIBLE);

		View btn_top_right_btn_wrap = (View)activity.findViewById(R.id.btn_top_right_btn_wrap);
		btn_top_right_btn_wrap.setVisibility(View.VISIBLE);

		// 오른쪽 상단 버튼을 스튜디오로 변경
		TextView btn_top_right_text = (TextView)activity.findViewById(R.id.btn_top_right_text);
		btn_top_right_text.setText("Studio");
		btn_top_right_text.setTextColor(activity.getResources().getColor(R.color.yellow));

		// 편집 안보이게
		FrameLayout btn_top_left_btn_wrap = (FrameLayout)activity.findViewById(R.id.btn_top_left_btn_wrap);
		btn_top_left_btn_wrap.setVisibility(View.GONE);


		// 스튜디오 가기
		ImageView btn_top_right_img = (ImageView) activity.findViewById(R.id.btn_top_right_img);
		btn_top_right_img.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				ImageView view = (ImageView)v;
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					view.setPadding(2, 2, 2, 2);
					view.setColorFilter(0xaa111111,Mode.SRC_OVER);				
				}else if(event.getAction() == MotionEvent.ACTION_UP){

					activity.displayStudio();

					view.setPadding(0, 0, 0, 0);
					view.setColorFilter(0x00000000, Mode.SRC_OVER);
				}else{
					view.setPadding(0, 0, 0, 0);
					view.setColorFilter(0x00000000, Mode.SRC_OVER);
				}    			

				return true;
			}
		});


		ViewFlipper myViewFlipper = (ViewFlipper)activity.findViewById(R.id.flipper_layout);

		View fromView = myViewFlipper.getChildAt(1); // studio
		View toView = myViewFlipper.getChildAt(0); // genre		
		AnimationFactory.flipTransition(myViewFlipper, fromView, toView, FlipDirection.RIGHT_LEFT);
	}

	public void onClick(View v,Messenger mMessenger,Messenger mService ){

		if(!Connectivity.isConnected(activity.getApplicationContext())){
			Toast.makeText(activity.getApplicationContext(), "Network is not available.", Toast.LENGTH_SHORT ).show();
			return;
		}

		MediaPlayerSingleton.whatKindOfNetwork = Connectivity.getWhatKindOfNetwork(activity.getApplicationContext());		
		Message msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_GENRE_PLAYLIST);

		try{
			switch(v.getId()){
			// channel
			case R.id.radio_every_music:
				msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_CH_PLAYLIST);
				msg.replyTo = mMessenger;
				msg.arg1 = 1; // type
				mService.send(msg);				
				break;
			case R.id.radio_ch_hotsong:
				msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_CH_PLAYLIST);
				msg.replyTo = mMessenger;
				msg.arg1 = 2; // type
				mService.send(msg);	
				break;
			case R.id.radio_ch_kpophotsong:
				msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_CH_PLAYLIST);
				msg.replyTo = mMessenger;
				msg.arg1 = 3; // type
				mService.send(msg);
				break;
			case R.id.radio_ch_bonoflowfm:
				msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_CH_PLAYLIST);
				msg.replyTo = mMessenger;
				msg.arg1 = 4; // type
				mService.send(msg);
				break;

			// genre
			case R.id.img_genre_dence:					
				msg.replyTo = mMessenger;
				msg.arg1 = 1; // type
				mService.send(msg);				
				break;					
			case R.id.img_genre_electronic:
				msg.replyTo = mMessenger;
				msg.arg1 = 2; // type
				mService.send(msg);	
				break;
			case R.id.img_genre_house:
				msg.replyTo = mMessenger;
				msg.arg1 = 3; // type
				mService.send(msg);
				break;
			case R.id.img_genre_pop:
				msg.replyTo = mMessenger;
				msg.arg1 = 4; // type
				mService.send(msg);
				break;
			case R.id.img_genre_rb:
				msg.replyTo = mMessenger;
				msg.arg1 = 5; // type
				mService.send(msg);
				break;
			case R.id.img_genre_jazz:
				msg.replyTo = mMessenger;
				msg.arg1 = 6; // type
				mService.send(msg);
				break;
			case R.id.img_genre_hiphop:
				msg.replyTo = mMessenger;
				msg.arg1 = 7; // type
				mService.send(msg);
				break;
			case R.id.img_genre_rock:
				msg.replyTo = mMessenger;
				msg.arg1 = 8; // type
				mService.send(msg);
				break;
			case R.id.img_genre_metal:
				msg.replyTo = mMessenger;
				msg.arg1 = 9; // type
				mService.send(msg);
				break;
			case R.id.img_genre_classical:
				msg.replyTo = mMessenger;
				msg.arg1 = 10; // type
				mService.send(msg);
				break;
			case R.id.img_genre_newage:
				msg.replyTo = mMessenger;
				msg.arg1 = 11; // type
				mService.send(msg);
				break;
			case R.id.img_genre_folk:
				msg.replyTo = mMessenger;
				msg.arg1 = 12; // type
				mService.send(msg);
				break;
			case R.id.img_genre_latin:
				msg.replyTo = mMessenger;
				msg.arg1 = 13; // type
				mService.send(msg);
				break;
			case R.id.img_genre_reggage:
				msg.replyTo = mMessenger;
				msg.arg1 = 14; // type
				mService.send(msg);
				break;
			case R.id.img_genre_reli:
				msg.replyTo = mMessenger;
				msg.arg1 = 15; // type
				mService.send(msg);
				break;
			case R.id.img_genre_other:
				msg.replyTo = mMessenger;
				msg.arg1 = 16; // type
				mService.send(msg);
				break;

			}		
			hide();

		}catch(Exception e){
			e.printStackTrace();
		}

	}

	@Override
	public void showMd(){
		View btn_top_left_btn_wrap = (View)activity.findViewById(R.id.btn_top_left_btn_wrap);
		btn_top_left_btn_wrap.setVisibility(View.GONE);
	}

	@Override
	public void backMd() {
		// TODO Auto-generated method stub
		activity.setCurrentMoudle(null);
		hide();		
	}

}
